package org.bangumibuddy.service.Impl;

import org.bangumibuddy.exception.rss.RssSubscribedException;
import org.bangumibuddy.parser.RSSParser;
import org.bangumibuddy.parser.factory.AnimeMetaParserFactory;
import org.bangumibuddy.parser.factory.RssParserFactory;
import org.bangumibuddy.pojo.TorrentFile;
import org.bangumibuddy.pojo.anime.Anime;
import org.bangumibuddy.service.MovieScrapingService;
import org.bangumibuddy.service.RssParserService;
import org.bangumibuddy.vo.SubscribeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class RssParserServiceImpl implements RssParserService {

    @Autowired
    AnimeMetaParserFactory animeMetaParserFactory;

    @Autowired
    MovieScrapingService movieScrapingService;

    @Override
    public SubscribeVO parseRss(String url) {
        RSSParser parser = RssParserFactory.create(url);
        RssFileCheck(parser);
        Anime anime = parser.getAnime();

        //刮削操作获取更多信息
        anime = UpdateWithScrapedData(anime);

        return getSubscribeVO(anime);
    }

    private Anime UpdateWithScrapedData(Anime anime) {
        List<String> animeZhName = movieScrapingService.getAnimeZhName(anime.getName(), anime.getSeasonNumber());
        if (animeZhName.size() == 1) {
            //修改保存路径
            List<TorrentFile> torrentFiles = anime.getTorrentFiles();
            for (TorrentFile torrentFile : torrentFiles) {
                if (torrentFile.getIsCollection()) {
                    torrentFile.setFileName(torrentFile.getTorrentName());
                    torrentFile.setSavePath(anime.getName() + "/S" + anime.getSeasonNumber());
                } else {
                    torrentFile.setFileName(anime.getName() + " S" + anime.getSeasonNumber() + "E" + torrentFile.getEpisode() + "." + torrentFile.getExtension());
                    torrentFile.setSavePath(anime.getName() + "/S" + anime.getSeasonNumber() + "/[" + torrentFile.getGroup() + "]");
                }
            }
            anime.setTorrentFiles(torrentFiles);
        }

        return anime;
    }

    private SubscribeVO getSubscribeVO(Anime anime) {
        SubscribeVO subscribeVO = new SubscribeVO();
        List<TorrentFile> torrentFiles = anime.getTorrentFiles();

        subscribeVO.setTitle(anime.getName());
        subscribeVO.setSeason(anime.getSeason());
        subscribeVO.setSeasonNumber(anime.getSeasonNumber());
        subscribeVO.setUrl(anime.getRssUrl());
        subscribeVO.setRssName(anime.getRssName());

        List<String> searchedName = movieScrapingService.getAnimeZhName(anime.getName(), anime.getSeasonNumber());
        subscribeVO.setSearchedName(searchedName);
        if(searchedName.size() == 1)
        {
            subscribeVO.setTitle(searchedName.get(0));
            subscribeVO.setSelectedSearchName(searchedName.get(0));
        }


        subscribeVO.setFileList(torrentFiles);
        subscribeVO.setIncludePatterns(anime.getIncludePatterns());
        subscribeVO.setExcludePatterns(anime.getExcludePatterns());

        return subscribeVO;
    }

    private void RssFileCheck(RSSParser parser) {
        if (parser.getTorrentFiles().isEmpty())
            throw new RssSubscribedException(parser.getUrl() + "内容为空！");
    }
}
